package cn.net.awit.API.util;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service("redisForValue")
public class RedisForValueImpl implements RedisForValue{
	
	@Autowired
    private StringRedisTemplate redisTemplate;
	
	/**
	 * 写入redis缓存（不设置expire存活时间）
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, String value)throws Exception{
		boolean result = false;
		try {
			ValueOperations operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			throw new Exception("写入redis缓存失败！错误信息为：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 写入redis缓存（设置expire存活时间）
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 */
	public boolean set(final String key, String value, int timeHour)throws Exception{
		boolean result = false;
		try {
			ValueOperations operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, timeHour, TimeUnit.HOURS);
			result = true;
		} catch (Exception e) {
			throw new Exception("写入redis缓存（设置expire存活时间）失败！错误信息为：" + e.getMessage());
		}
		return result;
	}
	
	
	/**
	 * 读取redis缓存
	 * @param key
	 * @return
	 */
	public Object get(final String key)throws Exception{
		Object result = null;
		try {
			ValueOperations operations = redisTemplate.opsForValue();
			result = operations.get(key);
		} catch (Exception e) {
			throw new Exception("读取redis缓存失败！错误信息为：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 判断redis缓存中是否有对应的key
	 * @param key
	 * @return
	 */
	public boolean exists(final String key)throws Exception{
		boolean result = false;
		try {
			result = redisTemplate.hasKey(key);
		} catch (Exception e) {
			throw new Exception("判断redis缓存中是否有对应的key失败！错误信息为：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * redis根据key删除对应的value
	 * @param key
	 * @return
	 */
	public boolean remove(final String key)throws Exception{
		boolean result = false;
		try {
			if(exists(key)){
				redisTemplate.delete(key);
			}
			result = true;
		} catch (Exception e) {
			throw new Exception("redis根据key删除对应的value失败！错误信息为：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * redis根据keys批量删除对应的value
	 * @param keys
	 * @return
	 */
	public void remove(final String... keys)throws Exception{
		for(String key : keys){
			remove(key);
		}
	}
}
